
clear

cd "D:\data_replication"

import delimited data\intermediates\intermediate_pc8plus.csv
drop product_id
save data\intermediates\intermediate_pc8plus.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc7 = substr(pc8plus, 1, 7)
sort pc7
by pc7: egen intermediate_pc7 = mean(intermediate)
by pc7: keep if _n == 1
gen intermediate_cons_pc7 = 0
replace intermediate_cons_pc7 = 1 if intermediate_pc7 == 1
gen intermediate_temp_pc7 = 0
replace intermediate_temp_pc7 = 1 if intermediate_pc7 > 0.5
keep pc7 intermediate_cons_pc7 intermediate_temp_pc7
rename intermediate_temp_pc7 intermediate_pc7
save data\intermediates\intermediate_pc7.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc6 = substr(pc8plus, 1, 6)
sort pc6
by pc6: egen intermediate_pc6 = mean(intermediate)
by pc6: keep if _n == 1
gen intermediate_cons_pc6 = 0
replace intermediate_cons_pc6 = 1 if intermediate_pc6 == 1
gen intermediate_temp_pc6 = 0
replace intermediate_temp_pc6 = 1 if intermediate_pc6 > 0.5
keep pc6 intermediate_cons_pc6 intermediate_temp_pc6
rename intermediate_temp_pc6 intermediate_pc6
save data\intermediates\intermediate_pc6.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc5 = substr(pc8plus, 1, 5)
sort pc5
by pc5: egen intermediate_pc5 = mean(intermediate)
by pc5: keep if _n == 1
gen intermediate_cons_pc5 = 0
replace intermediate_cons_pc5 = 1 if intermediate_pc5 == 1
gen intermediate_temp_pc5 = 0
replace intermediate_temp_pc5 = 1 if intermediate_pc5 > 0.5
keep pc5 intermediate_cons_pc5 intermediate_temp_pc5
rename intermediate_temp_pc5 intermediate_pc5
save data\intermediates\intermediate_pc5.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc4 = substr(pc8plus, 1, 4)
sort pc4
by pc4: egen intermediate_pc4 = mean(intermediate)
by pc4: keep if _n == 1
gen intermediate_cons_pc4 = 0
replace intermediate_cons_pc4 = 1 if intermediate_pc4 == 1
gen intermediate_temp_pc4 = 0
replace intermediate_temp_pc4 = 1 if intermediate_pc4 > 0.5
keep pc4 intermediate_cons_pc4 intermediate_temp_pc4
rename intermediate_temp_pc4 intermediate_pc4
save data\intermediates\intermediate_pc4.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc3 = substr(pc8plus, 1, 3)
sort pc3
by pc3: egen intermediate_pc3 = mean(intermediate)
by pc3: keep if _n == 1
gen intermediate_cons_pc3 = 0
replace intermediate_cons_pc3 = 1 if intermediate_pc3 == 1
gen intermediate_temp_pc3 = 0
replace intermediate_temp_pc3 = 1 if intermediate_pc3 > 0.5
keep pc3 intermediate_cons_pc3 intermediate_temp_pc3
rename intermediate_temp_pc3 intermediate_pc3
save data\intermediates\intermediate_pc3.dta, replace


use data\intermediates\intermediate_pc8plus.dta, clear
gen ll = length(pc8plus)
keep if ll == 8
gen pc2 = substr(pc8plus, 1, 2)
sort pc2
by pc2: egen intermediate_pc2 = mean(intermediate)
by pc2: keep if _n == 1
gen intermediate_cons_pc2 = 0
replace intermediate_cons_pc2 = 1 if intermediate_pc2 == 1
gen intermediate_temp_pc2 = 0
replace intermediate_temp_pc2 = 1 if intermediate_pc2 > 0.5
keep pc2 intermediate_cons_pc2 intermediate_temp_pc2
rename intermediate_temp_pc2 intermediate_pc2
save data\intermediates\intermediate_pc2.dta, replace



use data\intermediates\intermediate_pc8plus.dta, clear
rename pc8plus pc7
rename intermediate intermediate_pc7
gen intermediate_cons_pc7 = intermediate_pc7
append using data\intermediates\intermediate_pc7.dta
rename pc7 pc6
rename intermediate_pc7 intermediate_pc6
rename intermediate_cons_pc7 intermediate_cons_pc6
append using data\intermediates\intermediate_pc6.dta
rename pc6 pc5
rename intermediate_pc6 intermediate_pc5
rename intermediate_cons_pc6 intermediate_cons_pc5
append using data\intermediates\intermediate_pc5.dta
rename pc5 pc4
rename intermediate_pc5 intermediate_pc4
rename intermediate_cons_pc5 intermediate_cons_pc4
append using data\intermediates\intermediate_pc4.dta
rename pc4 pc3
rename intermediate_pc4 intermediate_pc3
rename intermediate_cons_pc4 intermediate_cons_pc3
append using data\intermediates\intermediate_pc3.dta
rename pc3 pc2
rename intermediate_pc3 intermediate_pc2
rename intermediate_cons_pc3 intermediate_cons_pc2
append using data\intermediates\intermediate_pc2.dta
rename pc2 pc
rename intermediate_pc2 intermediate_pc
rename intermediate_cons_pc2 intermediate_cons_pc
save data\intermediates\intermediate_pc.dta, replace


use estimation\2_product_list\output\product_id_all.dta, clear
merge m:1 pc using data\intermediates\intermediate_pc.dta

sort product_id pc8plus
drop if _merge == 2
replace intermediate_pc = 0 if intermediate_pc == .
replace intermediate_cons_pc = 0 if intermediate_cons_pc == .

drop _merge
rename intermediate_pc intermediate_pc_main
rename intermediate_cons_pc intermediate_cons_pc_main
rename pc pc_main
rename pc8plus pc
merge m:1 pc using data\intermediates\intermediate_pc.dta
drop if _merge == 2
sort product_id pc
replace intermediate_pc = 0 if intermediate_pc == . & _n > 3817
replace intermediate_cons_pc = 0 if intermediate_cons_pc == . & _n > 3817


sort product_id
by product_id: gen nr_pc_sub = _N
by product_id: egen intermediate_pc_sub = sum(intermediate_pc)
by product_id: egen intermediate_cons_pc_sub = sum(intermediate_cons_pc)
by product_id: keep if _n == 1

gen intermediate_product_id_t = intermediate_pc_main
gen intermediate_cons_product_id_t = intermediate_cons_pc_main
replace intermediate_product_id_t = (intermediate_pc_main + intermediate_pc_sub) / (nr_pc_sub + 1) if _n > 3817
replace intermediate_cons_product_id_t = (intermediate_cons_pc_main + intermediate_cons_pc_sub) / (nr_pc_sub + 1) if _n > 3817

gen intermediate_product_id = 0
replace intermediate_product_id = 1 if intermediate_product_id_t > 0.5
gen intermediate_cons_product_id = 0
replace intermediate_cons_product_id = 1 if intermediate_cons_product_id_t == 1

keep product_id intermediate_product_id intermediate_cons_product_id 
save data\intermediates\intermediate_product_id.dta, replace


rm data\intermediates\intermediate_pc8plus.dta
rm data\intermediates\intermediate_pc7.dta
rm data\intermediates\intermediate_pc6.dta
rm data\intermediates\intermediate_pc5.dta
rm data\intermediates\intermediate_pc4.dta
rm data\intermediates\intermediate_pc3.dta
rm data\intermediates\intermediate_pc2.dta
rm data\intermediates\intermediate_pc.dta


